#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
THN DATA ABSTRACTION LAYER version 1

Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

This library is dual-licensed.
  + For commercial software, please obtain a commercial license from THN Solutions LLC.

  + For free software, this library is licensed under GPL version 3. A summary of GPLv3 is
    listed below. You should also find a copy of GPLv3 as file License_GPLv3.txt included
	with the source-code files.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 3 as published by
    the Free Software Foundation  of the License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#endregion

#region Record of Change
//             							CHANGE HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | DEVELOPER  | DESCRIPTION                                              |
// -------------------------------------------------------------------------------------
// | 24-Apr-06 | HaiNM      | First creation.                                          |
// -------------------------------------------------------------------------------------
//
#endregion

#region Record of Audit
//             							REVIEW HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | REVIEWER   | NOTES                                                    |
// -------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------
//
#endregion

#region Using directives
using System;
using System.Collections;
using System.Windows.Forms;
using System.ComponentModel;

using Thn;
using Thn.Application.Logging;
#endregion

namespace Demo
{
    /// <summary>
    /// A simple listener for rendering logs.
    /// </summary>
    [ToolboxItem(true)]
#if DEBUG || TESTING
    public
#else
    public
#endif
 class LogListener : Component, ILogListener
    {
        #region Fields
        Stack mScopes = new Stack();
        int mIndentLevel = 0;
        #endregion

        #region Properties

        #region Viewer
        private ListBox mViewer;
        /// <summary>
        /// Gets or sets the target to render logs to.
        /// </summary>
        public ListBox Viewer
        {
            get { return mViewer; }
            set { mViewer = value; }
        }
        #endregion

        #endregion

        #region Indent
        void Indent()
        {
            mIndentLevel++;
        }
        #endregion

        #region Outdent
        void Outdent()
        {
            if (mIndentLevel > 0) mIndentLevel--;
        }
        #endregion

        #region Write
        void Write(string message)
        {
            string prefix = "";
            for (int i = 0; i < mIndentLevel; i++)
            {
                prefix += "   ";
            }

            message = prefix + message;
            mViewer.Items.Add(message);
            mViewer.SelectedIndex = mViewer.Items.Count - 1;
        }
        #endregion

        #region ILogListener Members

        #region Start
        void ILogListener.Start(string scopeName, object scopeID)
        {
            mScopes.Push(scopeID);
            Indent();
            Write(scopeName);
        }
        #endregion

        #region End
        void ILogListener.End(object scopeID)
        {
            var current = mScopes.Pop();
            Outdent();
            while (current != scopeID && mScopes.Count > 0)
            {
                //next iteration
                current = mScopes.Pop();
                Outdent();
            }
        }
        #endregion

        #region New Session
        void ILogListener.NewSession()
        {
            if (mViewer != null)
            {
                mViewer.Items.Clear();
            }
        }
        #endregion

        #region Receive Log
        void ILogListener.ReceiveLog(LogEvent log)
        {
            Write(log.Description);
        }
        #endregion

        #region Update
        void ILogListener.Update(object scopeID, string scopeName)
        {

        }
        #endregion

        #endregion

        #region Constructors
        /// <summary>
        /// Create a new instance.
        /// </summary>
        public LogListener()
            : base()
        {

        }
        #endregion

    }
}
